const utils = require('../../utils/util.js')
const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  data: {
    authorization: true,
    fromMine: true,
    fixedshow: false,
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    background: 'jsynewshop背景1.svg',
    redUrl: getApp().globalData.imgUrl + 'redCard.png',
    blackUrl: getApp().globalData.imgUrl + 'blackCard.png',
    isVip: false
  },
  setDefault(e) { //点击设为默认
    let ev = e.currentTarget.dataset
    if (ev.isdefault == 'false') {
      utils.API('spwx/v1.Vipcard/setDefault', {
        card_id: ev.card_id
      }).then(({
        code,
        msg
      }) => {
        if (code == 200) {
          this.require(ev.card_id)
        } else {
          wx.showToast({
            title: msg,
            icon: 'none'
          })
        }
      })

    }
  },
  jumpGrow(e) {
    wx.navigateTo({
      url: `../VipGrow/VipGrow?card_id=${e.currentTarget.dataset.card_id}`
    })
  },
  bindchange(e) {
    let index = e.detail.current,
      {
        noRecieveCards,
        alreadyRecieveCards,
        stasus
      } = this.data;

    if (stasus == "false") {
      this.require(noRecieveCards[index].card_id)
      this.data.card_id = noRecieveCards[index].card_id
    } else {
      this.require(alreadyRecieveCards[index].card_id)
      this.data.card_id = alreadyRecieveCards[index].card_id
    }
  },
  showHidden() {
    this.setData({
      fixedshow: !this.data.fixedshow
    })
  },
  jump(e) {
    wx.navigateTo({
      url: `../shopDetails/shopDetails?goods_id=${e.currentTarget.dataset.goods_id}`
    })
  },
  getVip(e) {
    if (e.currentTarget.dataset.caniclicked == 'allow') { //可以吊起支付
      this.pay()
    }
  },
  pay() { //为获取会员卡调去支付
    let openid = getApp().globalData.loginData.openid;
    utils.API('spwx/v1.Vipcard/receiveVipCard', {
      card_id: this.data.card_id,
      openid
    }).then(({
      code,
      data,
      msg
    }) => {
      if (code == 200) {
        if (data.order_id) {
          let { timeStamp, nonceStr, package, signType, paySign } = data.return
          wx.requestPayment({
            timeStamp,
            nonceStr,
            package,
            signType,
            paySign,
            success() {
              wx.showToast({
                title: '开卡成功！可以在个人中心，卡包里查看！',
                icon: 'none',
                duration: 2000
              })
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              }, 2000)
            },
            fail() {
              wx.showToast({
                title: '开卡失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else {
          wx.showToast({
            title: '领取成功！可以在个人中心，卡包里查看！',
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      } else {
        let that = this
        wx.showToast({
          title: msg + '',
          icon: 'none',
          success() {
            that.setData({
              authorization: false
            })
          }
        })
      }
    })
  },
  // 商品封面取第一张
  getFirstImg(trg) {
    if(trg.length != 0){
      trg = trg.map((val) => {
        val.goods_cover = val.goods_cover.split(',')[0]
        return val
      })
      return trg
    }
  },
  require(card_id) { //获取卡下面的页面信息
    utils.API('spwx/v1.Vipcard/details', {
      card_id,
    }).then(({
      code,
      data,
      msg
    }) => {
      if (code === 200) {
        let exclusiveGoods = data.exclusiveGoods,
          exclusive_shop = data.exclusive_shop;

          // 商品封面取第一张
          exclusiveGoods = this.getFirstImg(exclusiveGoods)
       
          // 会员专属商品封面取第一张
          exclusive_shop = this.getFirstImg(exclusive_shop)
        this.setData({
          data
        })
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })
  },
  getCard() { //获取卡信息
    utils.API('spwx/v1.Vipcard/cardBag').then(({
      code,
      msg,
      data
    }) => {
      if (code === 200) {
        let {
          noRecieveCards,
          alreadyRecieveCards
        } = data
        this.setData({
          noRecieveCards,
          alreadyRecieveCards
        })
        this.require(this.data.card_id)
      }else{
        wx.showToast({
          title:msg + '',
          icon:'none'
        })
      }
    })
  },
  onLoad(o) {
    this.data.card_id = o.card_id
    this.setData({
      cardIndex: o.index ? o.index : 0,
      stasus: o.stasus
    })
    this.getCard()
  },
  preventTouchMove (event) {
    return false
  },
}))